package com.feihong.txgw.shopcar.viewholder;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.shopcar.adapter.ShopCarOrderGoodsAdapter;
import com.feihong.txgw.view.NoScrollListView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.shopcar.viewholder
 * @class describe:  创建订单  商品的viewHolder
 * @author wangziheng
 * @time 2017/8/1 10:22
 * @chang time
 * @class describe
 */
public class CreateOrderGoodsViewHolder extends BaseViewHolder {

    public NoScrollListView mListView;
    public EditText mLeaveMsg;
    private ShopCarOrderGoodsAdapter mAdapter;
    private Context mContext;
    private List<ShopCarGoodsModel> dbGoodsCartModels;
//        public TextView mPrice_GoodsCount;
//        public TextView mPrice_GoodsPrice;

    public CreateOrderGoodsViewHolder(View itemView, Context context) {
        super(itemView);
        mContext = context;
        mListView = (NoScrollListView) itemView.findViewById(R.id.shopcar_order_goods_info_lv);
        mLeaveMsg = (EditText) itemView.findViewById(R.id.shopcar_createrOrder_lea_msg);
/*            mPrice_GoodsCount = (TextView) itemView.findViewById(R.id.shopcar_createOrder_Goods_price_count);
            mPrice_GoodsPrice = (TextView) itemView.findViewById(R.id.shopcar_createOrder_Goods_price_allCount);*/
        mAdapter = new ShopCarOrderGoodsAdapter(mContext);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 /*跳转到商品详情*/
                if (dbGoodsCartModels != null) {

                  /*  Intent intent = new Intent();
                    intent.setClass(mContext, ProductInfoActivity.class);
                    intent.putExtra(ProductInfoActivity.INTENT_PRODUCTID, dbGoodsCartModels.get(position).getGoodsId()+"");
                    mContext.startActivity(intent);*/
                }
            }
        });
    }

    public void initData(List<ShopCarGoodsModel> dbGoodsCartModels) {
        if (dbGoodsCartModels == null || dbGoodsCartModels.size() < 1) {
            return;
        }
        this.dbGoodsCartModels = dbGoodsCartModels;
        mListView.setAdapter(mAdapter);
        mAdapter.setData(dbGoodsCartModels);

        double allPrice = 0 ;
        int allNumGoods = 0 ;
        String strAllPrice = null;

        BigDecimal one = new BigDecimal("1");
        DecimalFormat df = new DecimalFormat("######0.00");
        for(ShopCarGoodsModel dbGoodsCartModel : dbGoodsCartModels){
            allNumGoods += dbGoodsCartModel.getGoodsNum();
            BigDecimal a1 = new BigDecimal(Double.toString(dbGoodsCartModel.getGoodsPrice()));
            BigDecimal a2 = new BigDecimal(Double.toString(dbGoodsCartModel.getGoodsNum()));
            BigDecimal result = a1.multiply(a2);
            allPrice += result.divide(one,2,BigDecimal.ROUND_HALF_UP).doubleValue();
        }
            strAllPrice  = df.format(allPrice);
      /*      mPrice_GoodsPrice.setText("¥"+strAllPrice);
            mPrice_GoodsCount.setText("共"+allNumGoods+"件商品  小计：");*/

    }
}
